package com.zqc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqc.entity.Category;
import com.zqc.entity.Dish;
import com.zqc.entity.Setmeal;
import com.zqc.exception.CustomException;
import com.zqc.mapper.CategoryMapper;
import com.zqc.service.CategoryService;
import com.zqc.service.DishService;
import com.zqc.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    public DishService dishService;

    @Autowired
    public SetMealService setMealService;

    /**
     * 根据id删除菜品
     *
     * @param id
     * @return
     */
    @Override
    public void remove(Long id) {
        // 1 查询菜品中是否关联该菜品分类，关联则不能删除
        long count = dishService.count(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, id));
        if (count > 0) {
            throw new CustomException("该菜品分类关联了菜品，不能直接删除");
        }
        // 2 查询套餐中是否关联该菜品分类，关联则不能删除
        long count1 = setMealService.count(new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getCategoryId, id));
        if (count1 > 0) {
            throw new CustomException("该菜品分类关联了套餐，不能直接删除");
        }
        // 3 正常删除
        this.removeById(id);
    }
}
